/**
 * Created by losor on 2017/3/5.
 */
public class _463_IslandPerimeter {
    public int islandPerimeter(int[][] grid) {
        int heigh = grid.length;
        int width = grid[0].length;

        int perimeter = 0;

        for (int i = 0; i < heigh; i++) {
            for (int j = 0; j < width; j++) {
                if(grid[i][j] == 1){
                    perimeter += 4;
                    if(i < heigh - 1){
                        if(grid[i+1][j] == 1){
                            perimeter -= 2;
                        }
                    }
                    if(j < width - 1){
                        if(grid[i][j+1] == 1){
                            perimeter -= 2;
                        }
                    }
                }
            }
        }

        return perimeter;
    }

}
